package com.fsu.database.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.SQLException;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.fsu.database.dao.DatabaseManager;

@SuppressWarnings("serial")
public class Splash extends JDialog implements LoginListener {

	private JLabel text;
	public Splash() {
		
		init();
		
		try {
			DatabaseManager.loadDriver();
			DatabaseManager.connect("FAIS002", "group2");
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(null, "SQL Error " + ex.getErrorCode() + ":" + ex.getMessage());
			ex.printStackTrace();
		} catch (ClassNotFoundException e) {
			JOptionPane.showMessageDialog(null, "No driver found...");
			e.printStackTrace();
		}
		
		text.setText("Checking credentials...");
		new LoginDialog(this, this);
	}
	
	private void init() {
		final Toolkit tk = Toolkit.getDefaultToolkit();
		final Dimension size = tk.getScreenSize();
		
		final JPanel mainPanel = new JPanel();
		text = new JLabel("Connecting...");
		mainPanel.add(text);
		add(mainPanel);
		
		setLocation(size.width/3, size.height/3);
		setSize(300, 100);
		setVisible(true);
	}
	
	@Override
	public void loginAttempt(boolean isAdmin) {
		text.setText("Starting...");
		new MainFrame(isAdmin);
		dispose();
	}

	@Override
	public void loginCanceled() {
		System.exit(0);
	}

}
